package com.pinusmusic.dao.impl;

import com.pinusmusic.dao.UserDao;
import com.pinusmusic.domain.Singer;
import com.pinusmusic.domain.User;
import com.pinusmusic.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public boolean findUserByUsernameAndUserpassword(String username, String userpassword) {
        String sql = "select * from user where username = ? and userpassword = ?";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), username, userpassword);
        if (list.size()==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean findUserByUsername(String username) {
        String sql = "select * from user where username = ?";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), username);
        if (list.size()==1){
            return true;
        }
        return false;
    }

    @Override
    public void insertUser(User user) {
        String sql = "insert into user values(null,?,?,?,?,?,?,null,?,null)";
        template.update(sql,user.getUsername(),user.getUserpassword(),user.getEmail(),user.getSex(),user.getTelephone(),user.getVipstatus(),user.getCommentstatus());
    }

    @Override
    public void updateInformation(User user) {
        String sql = "update user set userpassword = ? , sex = ? , email = ? , telephone = ? where username = ?";
        template.update(sql,user.getUserpassword(),user.getSex(),user.getEmail(),user.getTelephone(),user.getUsername());
    }

    @Override
    public User findByUserName(String name) {
        String sql="select * from user where username=?";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), name);
        if (list.size()==1){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateCodeByUsername(String code,String name) {
        String sql="update user set code=? where username=?";
        template.update(sql,code,name);
    }

    @Override
    public User findByEmail(String email) {
        String sql="select * from user where email=?";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), email);
        System.out.println(list);
        if (list.size()==1){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void  updatePwdByEmail(String newPwd,String email) {
        String sql="update user set userpassword=? where email=?";
        template.update(sql, newPwd, email);
    }

    @Override
    public void updateVipcodeByUsername(String uuid, String username) {
        String sql="update user set vipcode=? where username=?";
        template.update(sql,uuid,username);
    }

    @Override
    public User findByVipCode(String vipcode) {
        User user = null;
        try {
            String sql = "select * from user where vipcode = ?";
            user = template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class), vipcode);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }String sql = "update user set vipstatus = 'true' where uid = ?";
        template.update(sql,user.getUid());
        return user;
    }

    @Override
    public void updateVipStatus(User user) {
        String sql = "update user set vipstatus = 'true' where uid = ?";
        template.update(sql,user.getUid());
    }

    @Override
    public List<User> findUserByName(String username) {
        String sql = "select * from user where username = ?";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), username);
        return list;
    }

    @Override
    public List<User> findUsersByName(int start, int rows, String name) {
        if (name!=null && !name.trim().isEmpty() && !name.equals("")){
            String sql = "select * from user where 1=0";
            StringBuilder sb = new StringBuilder(sql);
            ArrayList<Object> params = new ArrayList<>();//存放musicName的值
            sb.append(" or username like ?");
            params.add("%"+name+"%");
            //分页
            sb.append("limit ?,?");
            params.add(start);
            params.add(rows);
            List<User> users = template.query(sb.toString(), new BeanPropertyRowMapper<>(User.class), params.toArray());
            return users.size()==0?null:users;
        }
        return null;
    }

    @Override
    public int getLikeCount(String name) {
        String sql = "select count(*) from user  where 1=0";
        StringBuilder sb = new StringBuilder(sql);
        ArrayList<Object> params = new ArrayList<>();//存放musicName的值
        if (name !=null && !name.trim().isEmpty()){
            sb.append(" or username like ?");
            params.add("%"+name+"%");
        }
        return template.queryForObject(sb.toString(),Integer.class,params.toArray());
    }

    @Override
    public User findUserById(Integer uid) {
        String sql = "select * from user where uid=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),uid);
    }
}
